/*********************************************
 * Copyright (c) 2009 ICBC.
 * All rights reserved.
 * Created on 2009-5-5 下午04:17:06
 * 
 * Contributors:
 *     ctp - initial implementation
 *********************************************/
package com.icbc.ctpdemo.uniform_attestation;

import java.security.PrivateKey;
import java.security.PublicKey;

import com.icbc.cte.base.CTEConstance;
import com.icbc.ssic.base.PKIException;
import com.icbc.ssic.base.SSICService;

public class SsicManager {

	private static SsicManager instance;

	// 统一认证密钥文件放置文件夹。当连真实统一认证环境，将密钥文件放置到此路径下面
	// 在模块配置文件中一般为${WEB_ROOT}/WEB-INF/config
	private String ssicKeyPath;
	// 统一认证服务器端密钥文件名
	private String ssicServerkeyName = "SSIC";
	// 统一认证客户端密钥文件名
	private String ssicClientKeyName = "ctp";
	// 密钥密码
	private String privKeyPasswd = "1a2b3c";
	// 时间间隔
	private String tsInterval = "600000000000";
	// 统一认证服务器地址
	private String ssicIp = "122.136.14.137:9080";
	// 统一认证客户机地址
	private String clientIp = "122.136.15.160:9081";
	// 统一认证版本
	private String ssicVersion = "1.0";
	// 公钥
	private PublicKey publicKey;
	// 私钥
	private PrivateKey privateKey;

	protected SsicManager() {

	}

	public static SsicManager getInstance() {
		if (instance == null) {
			instance = new SsicManager();
		}
		return instance;
	}

	public void initialize() throws PKIException {
		try {
			String ssicFlag = CTEConstance.getEnvProperty("enableSSIC");
			if ("true".equals(ssicFlag)) {
				SSICService ssic = new SSICService(getSsicServerkeyName());
				SSICService client = new SSICService(getSsicClientKeyName());
				client.initialize(getPrivKeyPasswd(), getSsicKeyPath());
				ssic.initialize(getSsicKeyPath());
				publicKey = ssic.getPublicKey();
				privateKey = client.getPrivateKey();
			}
		} catch (PKIException e) {
			throw e;
		}
	}

	public void terminate() {
		instance = null;
	}

	public void setSsicKeyPath(String ssicKeyPath) {
		this.ssicKeyPath = ssicKeyPath;
	}

	public String getSsicKeyPath() {
		return ssicKeyPath;
	}

	public void setSsicServerkeyName(String ssicServerkeyName) {
		this.ssicServerkeyName = ssicServerkeyName;
	}

	public String getSsicServerkeyName() {
		return ssicServerkeyName;
	}

	public void setSsicClientKeyName(String ssicClientKeyName) {
		this.ssicClientKeyName = ssicClientKeyName;
	}

	public String getSsicClientKeyName() {
		return ssicClientKeyName;
	}

	public void setPrivKeyPasswd(String privKeyPasswd) {
		this.privKeyPasswd = privKeyPasswd;
	}

	public String getPrivKeyPasswd() {
		return privKeyPasswd;
	}

	public void setTsInterval(String tsInterval) {
		this.tsInterval = tsInterval;
	}

	public String getTsInterval() {
		return tsInterval;
	}

	public void setSsicIp(String ssicIp) {
		this.ssicIp = ssicIp;
	}

	public String getSsicIp() {
		return ssicIp;
	}

	public void setClientIp(String clientIp) {
		this.clientIp = clientIp;
	}

	public String getClientIp() {
		return clientIp;
	}

	public void setSsicVersion(String ssicVersion) {
		this.ssicVersion = ssicVersion;
	}

	public String getSsicVersion() {
		return ssicVersion;
	}

	public PublicKey getPublicKey() {
		return publicKey;
	}

	public PrivateKey getPrivateKey() {
		return privateKey;
	}
}
